Data Types

This page has been superceded by a wiki version of this example: DataTypesExample

I'm going to start with the basics: whole numbers. This code will show the upper and lower limits for the integer data types in D and sizes for other data types. It's pretty fancy, so hopefully I'm doing this right. I make use of the min and max attributes.

Code

int main() 

{

    // Show information about integer types...

    printf("bit\tmin: %d\tmax: %d (%u)\n", bit.min, bit.max, bit.size);  

    printf("ubyte\tmin: %u\tmax: %u (%u)\n", ubyte.min, ubyte.max, ubyte.size);  

    printf("ushort\tmin: %u\tmax: %u (%u)\n", ushort.min, ushort.max, ushort.size);  

    printf("uint\tmin: %u\tmax: %u (%u)\n", uint.min, uint.max, uint.size);  

    printf("ulong\tmin: %llu\tmax: %llu (%u)\n", ulong.min, ulong.max, ulong.size);    

    printf("byte\tmin: %d\tmax: %d (%u)\n", byte.min, byte.max, byte.size);  

    printf("short\tmin: %d\tmax: %d (%u)\n", short.min, short.max, short.size);    

    printf("int\tmin: %d\tmax: %d (%u)\n", int.min, int.max, int.size);    

    printf("long\tmin: %lld\tmax: %lld (%u)\n", long.min, long.max, long.size);    



    // Show information about floating-point types...

    printf("float (%u)\tdouble (%u)\treal (%u)\t", float.size, double.size, real.size);

    printf("ifloat (%u)\tidouble (%u)\tireal (%u)\t", ifloat.size, idouble.size, ireal.size);

    printf("cfloat (%u)\tcdouble (%u)\tcreal (%u)\t", cfloat.size, cdouble.size, creal.size);

    printf("cfloat (%u)\tcdouble (%u)\tcreal (%u)\t", cfloat.size, cdouble.size, creal.size);



    // Show information about character types...

    printf("char (%u)\twchar (%u)\tdchar (%u)\t", char.size, wchar.size, dchar.size);



    return 0;

}

Output

bit     min: 0  max: 1 (1)

ubyte   min: 0  max: 255 (1)

ushort  min: 0  max: 65535 (2)

uint    min: 0  max: 4294967295 (4)

ulong   min: 0  max: 18446744073709551615 (8)

byte    min: -128       max: 127 (1)

short   min: -32768     max: 32767 (2)

int     min: -2147483648        max: 2147483647 (4)

long    min: -9223372036854775808       max: 9223372036854775807 (8)

float (4)       double (8)      real (10)       ifloat (4)      idouble (8)

ireal (10)      cfloat (8)      cdouble (16)    creal (20)      cfloat (8)

cdouble (16)    creal (20)      char (1)        wchar (2)       dchar (4)

Integer Types

TypeMinMaxSize (bytes)
bit 0 11
ubyte 0 2551
ushort 0 65,5352
uint 0 4,294,967,2954
ulong 0 18,446,744,073,709,551,6158
byte -128 1271
short -32,768 32,7672
int -2,147,483,648 2,147,483,6474
long -9,223,372,036,854,775,808 9,223,372,036,854,775,8078

Floating-Point Types

Character Types

The official list is in the D Specification, of course.


Return to Tutorial Overview